package com.voxel.sense.infomsg.module.map.monitor.traceRecord;

import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import cyy.example.com.facotry.model.api.response.MyTaskListInfoEntity;

import java.util.List;

import butterknife.BindView;

/**
 * Created by ChenYiYao on 2018/5/14.
 */

public class TaskAdapter extends RecyclerAdapter<MyTaskListInfoEntity.BodyBean.TaskInfoListBean> {
    int mSelectItem = -1;

    public TaskAdapter(List<MyTaskListInfoEntity.BodyBean.TaskInfoListBean> taskInfoListBeans, AdapterListener<MyTaskListInfoEntity.BodyBean.TaskInfoListBean> listener) {
        super(taskInfoListBeans, listener);
    }

    @Override
    protected int getItemViewType(int position, MyTaskListInfoEntity.BodyBean.TaskInfoListBean taskInfoListBean) {
        return R.layout.item_recycler_select_task;
    }

    @Override
    protected RecyclerAdapter.ViewHolder<MyTaskListInfoEntity.BodyBean.TaskInfoListBean> onCreateViewHolder(View root, int viewType) {
        return new ViewHolder(root);
    }

    @Override
    public void onBindViewHolder(RecyclerAdapter.ViewHolder<MyTaskListInfoEntity.BodyBean.TaskInfoListBean> holder, int position) {
        super.onBindViewHolder(holder, position);
        ViewHolder h = (ViewHolder) holder;

        if (mSelectItem == position) {
            h.cb_task.setChecked(true);
        } else {
            h.cb_task.setChecked(false);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        ViewHolder viewHolder = (ViewHolder) v.getTag(com.example.com.common.R.id.tag_recycler_holder);
        int adapterPosition = viewHolder.getAdapterPosition();
        if (adapterPosition == mSelectItem) {
            mSelectItem = -1;
        } else {
            mSelectItem = adapterPosition;
        }

        notifyDataSetChanged();
    }

    public int getSelectItem() {
        return mSelectItem;
    }



    public static class ViewHolder extends RecyclerAdapter.ViewHolder<MyTaskListInfoEntity.BodyBean.TaskInfoListBean> {
        @BindView(R.id.tv_desc)
        public TextView tv_desc; //任务描述
        @BindView(R.id.tv_type)
        public TextView tv_type;//任务紧急状态描述
        @BindView(R.id.tv_firm_name)
        public TextView tv_firm_name;//公司名称
        @BindView(R.id.iv_location_icon)
        public ImageView iv_location_icon;//定位坐标
        @BindView(R.id.tv_district)
        public TextView tv_district;//所在区区
        @BindView(R.id.tv_address)
        public TextView tv_address;//详细地址
        @BindView(R.id.iv_time_icon)
        public ImageView iv_time_icon;//时间的icon
        @BindView(R.id.tv_start_time)
        public TextView tv_start_time;//开始时间
        @BindView(R.id.tv_people_name)
        public TextView tv_people_name;//执行人
        @BindView(R.id.cb_task)
        public CheckBox cb_task;//执行人

        public ViewHolder(View itemView) {
            super(itemView);

        }

        @Override
        protected void onBind(MyTaskListInfoEntity.BodyBean.TaskInfoListBean data) {

            tv_desc.setText(data.getTaskType());
            if (!TextUtils.isEmpty(data.getTaskUrgentGrade())) {
                tv_type.setText(data.getTaskUrgentGrade());
                tv_type.setVisibility(View.VISIBLE);
            } else {
                tv_type.setVisibility(View.GONE);
            }


            tv_firm_name.setText(data.getTaskObject());
            tv_district.setText(data.getTaskAreaName());
            tv_address.setText(data.getTaskObjectAddress());
            tv_people_name.setText(data.getTaskExcutorName());
            tv_start_time.setText(data.getTaskStartTime() + " - " + data.getTaskEndTime());

        }
    }
}
